
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}

function addCookie(name,value,expireHours){
    var cookieString=name+"="+escape(value)+"; path=/";
    //判断是否设置过期时间
    if(expireHours>0){
        var date=new Date();
        date.setTime(date.getTime()+expireHours*3600*1000);
        cookieString=cookieString+"; expire="+date.toGMTString();
    }
    document.cookie=cookieString;

}

function getCookie(name){
    var strcookie=document.cookie;
    var arrcookie=strcookie.split("; ");
    for(var i=0;i<arrcookie.length;i++){
        var arr=arrcookie[i].split("=");
        if(arr[0]==name)return arr[1];
    }
    return "";
}

function delCookie(name){//删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getcookie(name);
    if(cval!=null) document.cookie= name + "="+cval+"; path=/;expires="+exp.toGMTString();
}


function contains(arr, str) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === str) {
            return true;
        }
    }
    return false;
}

/* 显示提示对话框 */
function showConfirmDialog(data,url,msg){
    layer.confirm('确认要'+msg+'吗？',function(index){
        postDelData("post",data,url,"",function(dataObj){
            var msg = msg+"成功";
            if( typeof dataObj == "object"){
                msg = dataObj.msg;
            }
            layer.msg(msg,{icon:1,time:1000},function(){
                location.replace(location.href);
            });
        });
    });
}

/*删除*/
function delDialog(data,url){
    layer.confirm('确认要删除吗？',function(index){
        postDelData("post",data,url,"",function(dataObj){
            var msg = "删除成功";
            if( typeof dataObj == "object"){
                msg = dataObj.msg;
            }
            layer.msg(msg,{icon:1,time:1000},function(){
                window.location.reload();
            });
        });
    });
}

function showLayerFull(title,url){
    var index = layer.open({
        type:2,
        title:title,
        content:url,
    });
    layer.full(index);
}


/* 显示弹出层 */
function showLayer(title,url,width,height){
    if( isEmptyOrNull(width) ){
        width = '';
    }
    if( isEmptyOrNull(height) ){
        height = '510';
    }
    layer_show(title,url,width,height);
}
function showTips(msg,callBack,data){
    if( data == '' || data == null ) {
        data = {};
    }
    if( typeof data == 'number'){
        var icon = data;
        data = {icon:icon,time:2000};
    }
    layer.msg(msg,data,callBack);
}

function showAlertTips(msg,callBack,data){
    if( data == '' || data == null ){
        /**
         * icon
         * 1 success
         * 2 error
         * 3 提示
         * 4 锁
         * 5 难过
         * 6 微笑
         */
        data = {icon:3,time:2000};
    }
    if( typeof data == 'number'){
        var icon = data;
        data = {icon:icon,time:2000};
    }
    layer.alert(msg,data,callBack);
}
function hideAlertTips(){
}

function isEmptyOrNull( str ){
    var isEmpty = false;
    if(str==null || str=='' ||str==undefined){
        isEmpty = true;
    }
    return isEmpty;
}

function ajaxAdminFileUpload(id_type,id,url,that,is_reload) {
    is_reload = (typeof is_reload != 'boolean') ? false : is_reload;
    var obj = $(that);
    $.ajaxFileUpload({
            url:url, //用于文件上传的服务器端请求地址
            secureuri: false, //是否需要安全协议，一般设置为false
            fileElementId: id_type, //文件上传域的ID
            dataType: 'json', //返回值类型 一般设置为json
            data:{id:id,file_key:id_type},
            success: function (data, status)  //服务器成功响应处理函数
            {
                showTips(data.msg);
                if( data.code>0 ){
                    $("input[name='"+id_type+"_hidden']").val(data.data.pic_path);
                    obj.next('img').attr('src',data.data.pic_url).show();
                    if( is_reload ){
                        window.location.reload();
                    }
                }
            },
            error: function (data, status, e)//服务器响应失败处理函数
            {
                showTips(e);
            }
        }
    );

}

function postData(method,url,params,callBack){
    if(method=='')method = "post";
    var dataStr = getDataStr(params);
    $.ajax({
        type:method,
        dataType:"json",
        url:url,
        data:dataStr,
        success:function(obj){
            if(typeof callBack== 'function'){
                callBack(obj);
            }
        }

    });
}

function postDataNoErrorAlert(method,url,params,jumpUrl,is_alert,error_jump_url,callBack,beforeSend_callBack,complete_callBack,error_callBack){
    if(method=='')method = "post";
    var dataStr = getDataStr(params);
    $.ajax({
        type:method,
        dataType:"json",
        url:url,
        data:dataStr,
        timeout:10000,
        beforeSend:function(XMLHttpRequest){
            if(typeof beforeSend_callBack== 'function'){
                beforeSend_callBack(XMLHttpRequest);
            }
        },
        success:function(obj){
            if(obj.code<0){
                if(error_jump_url){
                    window.location.href=error_jump_url;
                }
            }else{
                if(jumpUrl){
                    window.location.href=jumpUrl;
                }else{
                    if(is_alert){
                        showAlertTips(obj.msg);
                    }
                }
            }
            if(typeof callBack== 'function'){
                callBack(obj);
            }
        },
        complete:function(XMLHttpRequest, textStatus){
            if(typeof complete_callBack== 'function'){
                complete_callBack(XMLHttpRequest,textStatus);
            }
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
            if(typeof error_callBack== 'function'){
                error_callBack(XMLHttpRequest,textStatus,errorThrown);
            }else{
                showAlertTips("服务器繁忙，请稍后再试！");
            }
        },
    });
}

function postEditData(method,params,url,jumpUrl,callBack){
    if(method=='')method = "post";
    var dataStr = getDataStr(params);
    $.ajax({
        type:method,
        dataType:"json",
        url:url,
        data:dataStr,
        success:function(obj){
            if(obj.code == -10020){//字段验证
                showAlertTips(obj.msg);
            }else if(obj.code<0){
                showAlertTips(obj.msg);
            }else{
                showAlertTips(obj.msg);
                if(jumpUrl){
                    setTimeout(function() {
                        goTo(jumpUrl);
                    },1100);
                }
            }
            if(typeof callBack == 'function'){
                callBack(obj);
            }
        }
    });
}

function postDelData(method,params,url,jumpUrl,callBack){
    if(method=='')method = "post";
    var dataStr = getDataStr(params);
    $.ajax({
        type:method,
        dataType:"json",
        url:url,
        data:dataStr,
        success:function(obj){
            if(obj.code<0){
                showAlertTips(obj.msg,"",{icon:2,time:2000});
            }else{
                if(typeof callBack == 'function'){
                    callBack(obj);
                }
                if(jumpUrl){
                    goTo(jumpUrl);
                }
            }
        }
    });
}

/**
 * 获取参数 string
 * @param params
 * @returns {string}
 */
function getDataStr(params){
    var isObj = false;
    for(var key in params){
        if( typeof params[key] == 'object' ){
            isObj = true;
            break;
        }
    }
    if( isObj ){
        return params;
    }
    params['t'] = new Date().getTime();
    //params['token'] = setToken(params);
    var dataStr = '';
    for(var key in params){
        dataStr += key+"="+params[key]+"&";
    }
    dataStr = dataStr.substring(0,dataStr.length-1);
    return dataStr;
}


function goTo(url) {
    window.location.href = url;
}

